layui.use(['table', 'admin', 'ax'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;

    /**
     * 库位表管理
     */
    var BasicPosition = {
        tableId: "basicPositionTable"
    };

    /**
     * 库位物料表管理
     */
    var BasicPositionmaterial = {
        tableId: "basicPositionmaterialTable"
    };

    /**
     * 初始化表格的列
     */
    BasicPosition.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键'},
            {field: 'warehouseName', sort: true, title: '库房'},
            {field: 'positionAreaName', sort: true, title: '所属库区'},
            {field: 'positionNumber', sort: true, title: '库位编号'},
            {field: 'traySize', sort: true, title: '托存量'},
        ]];
    };

    /**
     * 点击查询按钮
     */
    BasicPosition.search = function () {
        var queryData = {};
        queryData['condition'] = $("#condition").val();
        queryData['positionAreaId'] = $("#positionAreaId").val();
        table.reload(BasicPosition.tableId, {
            where: queryData, page: {curr: 1},
             done:function () {
            }
        });
    };
    
    // 批量分配物料
    BasicPosition.btnAssign = function () {
    	var checkStatus = table.checkStatus('basicPositionTable');
        data2 = checkStatus.data;
        if (data2.length>0) {
            var ids = [];
            for (var i = 0; i < data2.length; i++) {
            	ids.push(data2[i].id);
			}
            layer.open({
                type: 2,
                title: '批量分配物料',
                area: ['950px', '550px'],
                offset: '100px',
                anim: 5,
                content: Feng.ctxPath + '/basicPositionmaterial/toAssignMaterial?ids=' + ids,
                end: function () {
                    table.reload(BasicPositionmaterial.tableId);
                }
            });
		} else {
			layer.alert('请至少选中一条数据');
		}
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + BasicPosition.tableId,
        url: Feng.ctxPath + '/basicPosition/list',
        page: true,
        limit: 60,
		limits:[60,120,240,360,480,1500,5000],
        height: 410,
        toolbar: '#toolbarfromPosition',
        cellMinWidth: 100,
        cols: BasicPosition.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearchPosition').click(function () {
        BasicPosition.search();
    });
    
    // 批量分配物料
    $('#btnAssign').click(function () {
    	BasicPosition.btnAssign();
    });

    /**
     * 初始化表格的列
     */
    BasicPositionmaterial.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键'},
            {field: 'positionNumber', sort: true, title: '库位编号'},
            {field: 'materialNumber', sort: true, title: '物料编码'},
            {field: 'materialName', sort: true, title: '物料描述'},
            {field: 'bigType', sort: true, title: '物料大类'},
            {field: 'goodsType', sort: true, title: '物料类别'},
            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    // 单击行事件
    table.on('row(' + BasicPosition.tableId + ')', function (obj) {
        var data = obj.data;
        material(data);
    });
    
    function material(data){
    	var positionId;
    	if (data!=null) {
    		positionId = data.id;
		} else {
			positionId = 0;
		}
    	/**
         * 点击查询按钮
         */
        BasicPositionmaterial.search = function () {
            var queryData = {};
            queryData['condition'] = $("#condition2").val();
            table.reload(BasicPositionmaterial.tableId, {
                where: queryData, page: {curr: 1},
                 done:function () {
                     // 搜索按钮点击事件
                     $('#btnSearch').click(function () {
                         BasicPositionmaterial.search();
                     });
                }
            });
        };

        /**
         * 点击删除
         *
         * @param data 点击按钮时候的行数据
         */
        BasicPositionmaterial.onDeleteItem = function (data) {
            var operation = function () {
                var ajax = new $ax(Feng.ctxPath + "/basicPositionmaterial/delete", function (data) {
                    Feng.success("删除成功!");
                    table.reload(BasicPositionmaterial.tableId);
                }, function (data) {
                    Feng.error("删除失败!" + data.responseJSON.message + "!");
                });
                ajax.set("id", data.id);
                ajax.start();
            };
            Feng.confirm("是否删除?", operation);
        };

        // 渲染表格
        var tableResult = table.render({
            elem: '#' + BasicPositionmaterial.tableId,
            url: Feng.ctxPath + '/basicPositionmaterial/list?positionId='+positionId,
            page: true,
	        limit: 40,
            height: 390,
            toolbar: '#toolbarfrom',
            cellMinWidth: 100,
            cols: BasicPositionmaterial.initColumn()
        });

        // 搜索按钮点击事件
        $('#btnSearch').click(function () {
            BasicPositionmaterial.search();
        });

        // 工具条点击事件
        table.on('tool(' + BasicPositionmaterial.tableId + ')', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;

            if (layEvent === 'delete') {
                BasicPositionmaterial.onDeleteItem(data);
            }
        });
    };

    
});
